﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.DB;
using System.Net.Mail;
using PMPUtils.Resources;
using PMPUtils.Util;

namespace PMPUtils.Documentation
{
    public class DetailDesignOperator
    {
        public static DataTable ReadDetailDesignsInReview(String goalID)
        {
            return ReadDetailDesigns(goalID, "1");
        }

        public static DataTable ReadDetailDesignsNotInReview(String goalID)
        {
            return ReadDetailDesigns(goalID, "0");
        }

        public static DataTable ReadDetailDesigns(String goalID, String inReview)
        {
            String strQuery = "SELECT [author]      ,[goal_id]      ,[Rev]      ,[Design]      ,[ID]  FROM [DetailDesign] where to_review = " + inReview + " and goal_id = " + goalID + " order by id desc";
            DBOperator dbOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);

            DataTable dtDetailDesignsInReview = dbOperator.Read(strQuery);
            return dtDetailDesignsInReview;
        }

        public static void SubmitToReview(String selectedReviewers,
            String subject, String sender, String body)
        {
            MailAddressCollection reviewersCollection = new MailAddressCollection();
            if (selectedReviewers != null)
            {
                String[] reviewers = selectedReviewers.Split(';');

                for (int i = 0; i < reviewers.Length; i++)
                {
                    String reviewer = reviewers[i];
                    MailAddress reviewEmail = ResourceOperator.GetEmailAddress(reviewer);
                    reviewersCollection.Add(reviewEmail);
                }
            }

            // Send email
            MailAddress mailFrom = ResourceOperator.GetEmailAddress(sender);
            reviewersCollection.Add(mailFrom);

            String managerEmail = ResourceOperator.ReadManagerEmailByEmail(sender);
            if (!managerEmail.Equals(String.Empty))
            {
                MailAddress manager = new MailAddress(managerEmail);
                reviewersCollection.Add(manager);
            }

            // Add James and Prince
            MailAddress rain = new MailAddress("rain.wei@bleum.com");
            reviewersCollection.Add(rain);

            MailAddress michael = new MailAddress("michael.ji@bleum.com");
            MailAddress james = new MailAddress("james.shi@bleum.com");
            MailAddress prince = new MailAddress("prince.wang@bleum.com");
            reviewersCollection.Add(michael);
            reviewersCollection.Add(james);
            reviewersCollection.Add(prince);

            EmailOperator.Send(subject, body, reviewersCollection, mailFrom, true, null);
        }
    }
}
